isLogin()

//计算小计和总价的钱，并渲染
function xiaoji() {
    let num = 0
    $('.col-total1').each(function() {
        $(this).text(
            $(this).siblings('.col-price1').text().replace(/[^0-9]/ig,'') * $(this).prev().find('input').val() + '元'
        )
        if($(this).siblings('.col-check').children().prop('checked')) {
            num += $(this).text().replace(/[^0-9]/ig,'') * 1
        }
    })
    //渲染总价
    $('.total-price>em').text(num)
}

//渲染件数
function jianshu() {
    let num = 0,
        sum = 0
    $('.check-dx').each(function() {
        let inpnum = $(this).parent().siblings('.col-num').find('input').val()*1
        sum += inpnum
        if($(this).prop('checked')) {
            let inpnum = $(this).parent().siblings('.col-num').find('input').val()*1
            num += inpnum
        }
    })
    $('#cartTotaNum').text(sum)
    $('#selTotaNum').text(num)
}

function foodSwitch() {
    //绑定增加事件
    $('.item-box').find('.add-num').click(function() {
        let num = $(this).prev().val() * 1 + 1
        if(num >= 99) num = 99
        //  更新数据库的数量
        updataFood(num, $(this).data('id'))
        
        $(this).prev().val(num)
        xiaoji()
        jianshu()
    })
    //绑定增加减少事件
    .siblings('.reduce-num').click(function() {
        let num = $(this).next().val() * 1 - 1
        if(num <= 1) num = 1
        //  更新数据库的数量
        updataFood(num, $(this).data('id'))

        
        $(this).next().val(num)
        xiaoji()
        jianshu()
    })
    //绑定输入框事件
    .next().bind('input propertychange', function() {
        //  更新数据库的数量
        updataFood($(this).val(), $(this).data('id'))
        xiaoji()
        jianshu()
    })
    //绑定删除事件
    $('.col-action>a').click(function() {
        // 删除数据库数据
        removeFood($(this).data('id'))
        $(this).parents('.item-box').hide(500, function() {
            $(this).remove()
            xiaoji()
            jianshu()
        })
    })

    //全选按钮
    $('.check-qx').click(function() {
        $('.check-dx').prop('checked', this.checked)
        xiaoji()
        jianshu()
    })

    //复选按钮
    $('.check-dx').click(function() {
        let flag = true
        $('.check-dx').each(function() {
            if(!$(this).prop('checked')) flag = false
        })
        $('.check-qx').prop('checked', flag)
        xiaoji()
        jianshu()
    })
    xiaoji()
    jianshu()
}

//  渲染商品
async function applyFood(json) {
    let str = ''
    //  不能用foreach  要用for循环
    for(let i = 0; i < json.length; i++) {
        let data = await getJsonData(json[i].foodId)
        // console.log(data)
        str += `
        <div class="box-nb item-box">
            <div class="box-nb item-table">
                <div class="box-nb col col-check fl">
                    <input class="box-nb check-dx check-wd" type="checkbox">
                </div>
                <div class="box-nb col col-img fl"><img src="${data.img}" alt=""></div>
                <div class="box-nb col fl col-name">${data.title}</div>
                <div class="box-nb col col-price fl col-price1">${data.price}元</div>
                <div class="box-nb col fl col-num">
                    <div class="box-nb change-num">
                        <a class="box-nb reduce-num" href="javascript:;" data-id="${json[i].foodId}">-</a>
                        <input type="text"  maxlength="2" value="${json[i].foodNum}" data-id="${json[i].foodId}" class="box-nb goods-num">
                        <a class="box-nb add-num" href="javascript:;" data-id="${json[i].foodId}">+</a>
                    </div>
                </div>
                <div class="box-nb col fl col-total col-total1"></div>
                <div class="box-nb col fl col-action"><a href="javascript:;" data-id="${json[i].foodId}">x</a></div>
            </div>
        </div>
        `
    }
    $('.list-body').html(str)
    foodSwitch()
}

//  数据库拿数据
$.ajax({
    url: '/foods/query',
    type: 'POST',
    data: {
        userId: localStorage.getItem('user_id')
    },
    success: function(res) {
        if(res.code === 0) {
            // console.log(res.data)
            applyFood(res.data)
        }
    }
})
// json拿数据
function getJsonData(id) {
    return new Promise((resolve, reject) => {
        $.ajax({
            url: 'assets/js/food.json',
            type: 'GET',
            success: function(res) {
                res.forEach(item => {
                    
                    if(item.id == id) {
                        let data = {
                            price: item.price,
                            img: item.img,
                            title: item.title
                        }
                        resolve(data)
                    }
                })
            }
        })
    })
}

// 更新数据
function updataFood(num, foodId) {
    $.ajax({
        url: '/foods/update',
        type: 'POST',
        data: {
            userId: localStorage.getItem('user_id'),
            foodId,
            foodNum: num
        }
    })
}
//  删除数据
function removeFood(foodId) {
    $.ajax({
        url: '/foods/remove',
        type: 'POST',
        data: {
            userId: localStorage.getItem('user_id'),
            foodId
        }
    })
}